// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store/store'
import myinfo from './store/myinfo'
Vue.use(myinfo)
Vue.prototype.$myInfo=myinfo

import elementUI from 'element-ui'
Vue.use(elementUI)
import 'element-ui/lib/theme-chalk/index.css'

import vueCookies from 'vue-cookies'
Vue.use(vueCookies)

import axios from 'axios'
Vue.prototype.$axios=axios

//配置允许前端操作Cookie
axios.defaults.withCredentials=true

Vue.config.productionTip = false

//做路由拦截
router.beforeEach((to,from,next)=>{

   if(to.meta.require){
     let uuu=JSON.parse(sessionStorage.getItem("state"));
     if(uuu==null){
       next({path:"/"});
     }
     let urlList=uuu.urlList;
     if(urlList!=null&&urlList.indexOf(to.path)!=-1){
        next();
      }else {
        //跳转回原界面
        next({path:from.path});
      }
   }else{
     next();
   }
})


//自定义指令
Vue.directive("quanxian",{
  inserted:function (el,bind,vnode) {
    let quanxian=bind.value;

    let uuu=JSON.parse(sessionStorage.getItem("state"));
    let urlList=uuu.urlList;
    if(urlList.indexOf(quanxian)==-1){
      el.style.display="none";
    }
  }
})

//axios的请求拦截器
axios.interceptors.request.use((config)=>{

  return config;
})

//axios的响应拦截器
axios.interceptors.response.use((response)=>{

  if(response.data.code==900) {//未登录
    elementUI.Notification({title: '警告',message: '未登录或者登录过期',type: 'warning',duration: 2000})
    router.push({path:"/"})
  }
  if(response.data.code==901) {//用户名或者密码错误
    elementUI.Notification({title: '错误',message: '用户名或者密码错误',type: 'error',duration: 2000})
  }
  if(response.data.code==902) {//无权限
    elementUI.Notification({title: '警告',message: '用户无权限',type: 'warning',duration: 2000})
  }

  return response;
})


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})



